from rj2205.courses.course import Course


class CourseManger:
    def __init__(self):
        self.courses = []

    def run(self):
        self.load()
        while True:
            try:
                self.question()
                i = int(input("What function do you want to use"))
                if i == 1:
                    self.add()
                elif i == 2:
                    self.delete()
                elif i == 3:
                    self.upd()
                elif i == 4:
                    self.select_single()
                elif i == 5:
                    self.select_all()
                elif i == 6:
                    self.save()
                else:
                    print("Unknown answer, please enter again")
                    self.question()
                    self.run()
            except ValueError:
                print("Unknown answer, please enter again")
                self.question()
                self.run()

    def question(self):
        print("1.Add a new course's information\n"
              "2.Delete an existing course's information\n"
              "3.Update an existing course's information\n"
              "4.View single course's information\n"
              "5.View all course's information\n"
              "6.Save course's information into data\n")

    def add(self):
        c = True
        c_id = input("Enter course's id: ")
        for i in self.courses:
            if i.course_id == c_id:
                c = False
                print("This course is exist,please enter new course's id")

        if c:
            c_n = input("Enter course's name: ")
            for i in self.courses:
                if i.course_name == c_n:
                    c = False
                    print("This course is exist,please enter new course's name")
                    break
            if c:
                print("Enter course's level: ")
                clv = int(input("1.Base   2.Core  3.Choose"))
                c_lv = ""
                if clv == 1:
                    c_lv = "base"
                    new_course = Course(c_id, c_n, c_lv)
                    self.courses.append(new_course)
                    print("Add success")
                elif clv == 2:
                    c_lv = "core"
                    new_course = Course(c_id, c_n, c_lv)
                    self.courses.append(new_course)
                    print("Add success")
                elif clv == 3:
                    c_lv = "choose"
                    new_course = Course(c_id, c_n, c_lv)
                    self.courses.append(new_course)
                    print("Add success")
                else:
                    print("Unknown answer, please enter again")

    def delete(self):
        c = 0
        for i in self.courses:
            c += 1
        if c <= 3:
            print("The information is not enough, can't delete more")
        else:
            d_id = input("Please enter ID what you want to delete")
            for i in self.courses:
                if i.course_id == d_id:
                    self.courses.remove(i)
                    print("Delete successful")

    def upd(self):
        u_id = input("Please enter ID what you want to update")
        c = 0
        for i in self.courses:
            c += 1
            if i.course_id == u_id:
                c -= 1
                i.course_lv = input("Enter course's level: ")
                self.select_for_c(u_id)
            if c == self.courses.__len__():
                print("!!!Not find this student!!!")

    def select_single(self):
        if len(self.courses) == 0:
            print("!!!List is null, no data!!!")
        else:
            ssid = input("Please enter course's id what you want to select")
            self.select_for_c(ssid)

    def select_all(self):
        if len(self.courses) == 0:
            print("!!!List is null, no data here!!!")
        else:
            for i in self.courses:
                print(i)

    def save(self):
        LCB = 0
        LCCo = 0
        LCCh = 0
        for i in self.courses:
            if i.course_lv == "base":
                LCB += 1
            if i.course_lv == "core":
                LCCo += 1
            if i.course_lv == "choose":
                LCCh += 1
        if LCB == 0 or LCCo == 0 or LCCh == 0:
            print("Data is incomplete, please add more data")
        else:
            f = open("c.data", "w", encoding="utf-8")
            new_list = [i.__dict__ for i in self.courses]
            a = str(new_list)
            f.write(a)
            f.close()

    def load(self):
        try:
            f = open("../courses/c.data", "r", encoding="utf-8")
        except:
            f = open("../courses/c.data", "w", encoding="utf-8")
            a = '[]'
            f.write(a)
        else:
            data = f.read()
            new_list = eval(data)
            self.courses = [Course(i["course_name"], i["course_id"], i["course_lv"], ) for i in new_list]
            f.close()

    def select_for_c(self, u_id):
        c = 0
        for i in self.courses:
            c += 1
            if i.course_id == u_id:
                c -= 1
                print(i)
            if c == self.courses.__len__():
                print("!!!Not find this student!!!")
